package lumis.portal.sendmail.dao.hibernate;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

import lumis.portal.sendmail.IMailBody;
import lumis.portal.sendmail.IMailDestination;
import lumis.portal.sendmail.IMultipleMail;
import lumis.portal.sendmail.ISingleMail;

/**
 * Implementation of {@link ISingleMail} and {@link IMultipleMail} used by
 * {@link SendMailDaoHib}.
 * 
 * @version $Revision: 7955 $ $Date: 2007-08-14 13:37:30 -0300 (Tue, 14 Aug 2007) $
 * @since 4.0.4
 */
class Mail extends MailSendStatus implements IMultipleMail, ISingleMail
{
	private String id;

	private String charset = "UTF-8";

	private String subject;

	private String to;

	private String from;

	private String cc;

	private String bcc;

	private String replyTo;

	private String sourceComponent;

	private String sourceComponentStringsUrl;

	private Date requestDateTime;

	private MailQueue mailQueue;

	private IMailBody body = new MailBody();

	private Collection<IMailDestination> destinations;

	public String getSourceComponent()
	{
		return sourceComponent;
	}

	public void setSourceComponent(String sourceComponent)
	{
		this.sourceComponent = sourceComponent;
	}

	public String getSourceComponentStringsUrl()
	{
		return sourceComponentStringsUrl;
	}

	public void setSourceComponentStringsUrl(String sourceComponentStringsUrl)
	{
		this.sourceComponentStringsUrl = sourceComponentStringsUrl;
	}

	public MailQueue getMailQueue()
	{
		return mailQueue;
	}

	public void setMailQueue(MailQueue mailQueue)
	{
		this.mailQueue = mailQueue;
	}

	public Date getRequestDateTime()
	{
		return requestDateTime;
	}

	public void setRequestDateTime(Date requestDate)
	{
		this.requestDateTime = requestDate;
	}

	public Collection<IMailDestination> getDestinations()
	{
		if (destinations == null)
			setDestinations(new ArrayList<IMailDestination>());
		
		return new SetParentCollectionWrapper<IMailDestination>(destinations, 
				new SetParentCollectionWrapper.IParentSetter<IMailDestination>()
				{
					public void setParent(IMailDestination bean)
					{
						((MailDestination)bean).setMail(Mail.this);
					}
				});
	}

	public void setDestinations(Collection<IMailDestination> destinations)
	{
		this.destinations = destinations;
	}

	public IMailBody getBody()
	{
		return body;
	}

	public void setBody(IMailBody body)
	{
		this.body = body;
	}

	public String getCharset()
	{
		return charset;
	}

	public void setCharset(String charset)
	{
		this.charset = charset;
	}

	public String getBcc()
	{
		return bcc;
	}

	public void setBcc(String bcc)
	{
		this.bcc = bcc;
	}

	public String getCc()
	{
		return cc;
	}

	public void setCc(String cc)
	{
		this.cc = cc;
	}

	public String getFrom()
	{
		return from;
	}

	public void setFrom(String from)
	{
		this.from = from;
	}

	public String getId()
	{
		return id;
	}

	public void setId(String id)
	{
		this.id = id;
	}

	public String getReplyTo()
	{
		return replyTo;
	}

	public void setReplyTo(String replyTo)
	{
		this.replyTo = replyTo;
	}

	public String getSubject()
	{
		return subject;
	}

	public void setSubject(String subject)
	{
		this.subject = subject;
	}

	public String getTo()
	{
		return to;
	}

	public void setTo(String to)
	{
		this.to = to;
	}

	public int getMailType()
	{
		if (destinations == null || destinations.isEmpty())
			return MAIL_TYPE_SINGLE;
		else
			return MAIL_TYPE_MULTIPLE;
	}

	/**
	 * Returns the given string or an empty string if it is null.
	 * 
	 * @param str
	 *            the given string.
	 * @return the given string or an empty string if it is null.
	 * @since 4.0.4
	 */
	private String emptyStringIfNull(String str)
	{
		return str != null ? str : "";
	}

	public String getMailLoggingString()
	{
		return "Mail[" + getId() + "]: Subject \"" + emptyStringIfNull(getSubject()) + "\"; From \"" + emptyStringIfNull(getFrom()) + "\"; To \"" + emptyStringIfNull(getTo()) + "\"; Cc \""
				+ emptyStringIfNull(getCc()) + "\"; Bcc \"" + emptyStringIfNull(getBcc()) + "\"; Reply-To \"" + emptyStringIfNull(getReplyTo()) + "\"";
	}
}
